﻿using AutoMapper;
using MediatR;
using S.Breeding.Api.Application.Command.OustManageCommand;
using S.Breeding.Domain;
using S.Breeding.Infrastructure;

namespace S.Breeding.Api.Application.CommandHandlers.OustManageCommandHandlers
{
    public class OustManageCreateCommandHandlers : IRequestHandler<OustManageCreateCommand, int>
    {
        private readonly IRepository<OustManage> repository;
        private readonly IMapper mapper;
        public OustManageCreateCommandHandlers(IRepository<OustManage> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(OustManageCreateCommand request, CancellationToken cancellationToken)
        {
            var result = repository.AllAsync().Result.Where(x => x.Earbugles == request.Earbugles && x.OMId != request.OMId);
            if (result.Count() != 0)
            {
                return -1;
            }
            request.CreateBy = "年鹏";
            request.CreateDate = DateTime.Now;
            return await repository.CreateAsync(mapper.Map<OustManage>(request));
        }
    }
}
